package com.yidi.system.repository;

import com.yidi.common.core.Repository.BaseRepository;
import com.yidi.common.core.domain.entity.SysMenu;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author ：Xn
 * @ClassName: SysMenuRepository
 * @date ：2021-10-19 14:42
 * @describe ：菜单
 */
@Repository
public interface SysMenuRepository extends BaseRepository<SysMenu,Long> {

    @Query(nativeQuery = true, value = "select distinct m.id, m.parent_id as parentId, m.menu_name as menuName, m.path, m.component, m.query, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame as isFrame, m.is_cache as isCache, m.menu_type as menuType, m.icon, m.order_num as orderNum, m.create_time as createTime" +
            " from sys_menu m where m.menu_type in ('M', 'C') and m.status = 0 order by m.parent_id, m.order_num" )
    List<Map<String, Object>> selectMenuTreeAll();

    @Query(nativeQuery = true, value = "select distinct m.id, m.parent_id as parentId, m.menu_name as menuName, m.path, m.component, m.query, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame as isFrame, m.is_cache as isCache, m.menu_type as menuType, m.icon, m.order_num as orderNum, m.create_time as createTime" +
            " from sys_menu m" +
            " left join sys_role_menu rm on m.id = rm.menu_id" +
            " left join sys_user_role ur on rm.role_id = ur.role_id" +
            " left join sys_role ro on ur.role_id = ro.id" +
            " left join sys_user u on ur.user_id = u.id" +
            " where u.id = ?1 and m.menu_type in ('M', 'C') and m.status = 0  and ro.status = 0" +
            " order by m.parent_id, m.order_num")
    List<Map<String, Object>> selectMenuTreeByUserId(Long userId);

    @Query(nativeQuery = true, value = "select distinct m.id, m.parent_id as parentId, m.menu_name as menuName, m.path, m.component, m.query, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame as isFrame, m.is_cache as isCache, m.menu_type as menuType, m.icon, m.order_num as orderNum, m.create_time as createTime" +
            " from sys_menu m" +
            " left join sys_role_menu rm on m.id = rm.menu_id" +
            " left join sys_user_role ur on rm.role_id = ur.role_id" +
            " left join sys_role ro on ur.role_id = ro.id" +
            " where ur.user_id = ?1" +
            " and (m.menu_name like concat('%', ?2, '%') or ?2 is null)" +
            " and (m.visible = ?3 or ?3 is null)" +
            " and (m.status = ?4 or ?4 is null)" +
            " order by m.parent_id, m.order_num")
    List<Map<String, Object>> selectMenuListByUserId(Long userId, String menuName, String visible, String status);

    SysMenu findByMenuNameAndParentId(String menuName, Long parentId);

    @Query(nativeQuery = true, value = "select count(1) from sys_menu where parent_id = ?1")
    int hasChildByMenuId(Long menuId);

    @Query(nativeQuery = true, value = "select distinct m.perms from sys_menu m" +
            " left join sys_role_menu rm on m.id = rm.menu_id" +
            " left join sys_user_role ur on rm.role_id = ur.role_id" +
            " left join sys_role r on r.id = ur.role_id" +
            " where m.status = '0' and r.status = '0' and ur.user_id = ?1")
    List<String> selectMenuPermsByUserId(Long userId);
}
